package com.example.demo;

import com.example.demo.component.UserComponent;
import com.example.demo.configuration.UserConfiguration;
import com.example.demo.configuration.UserInfo;
import com.example.demo.controller.UserController;
import com.example.demo.respository.UserResposittory;
import com.example.demo.service.UserService;
import lombok.val;
import org.apache.catalina.User;
import org.apache.catalina.startup.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public DemoApplication() {
    }

    /**
     * 获取名字的规则
     * 1. 比如 UserService 获取名字为 userService 小驼峰
     * 2. 比如 UService 获取名字为 UService , 类名前两个字母都为大写,就直接返回类名
     */
    public static void main(String[] args) {
        // ApplicationContext 上下文
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        /**
         * Bean 练习
         */
        /*
        // 当有两个一个类有两个Bean, 我们只能通过 名称 或者 名称 + 类型来拿到Bean, 来确保能拿到Bean
        UserInfo userInfo = (UserInfo) context.getBean("userInfo",UserInfo.class);
        System.out.println(userInfo.toString());
         */

        /**/
        // 获取userInfo对象
//        UserInfo userInfo = (UserInfo) context.getBean("u3");
//        System.out.println(userInfo.toString());


        // 报错 No qualifying bean of type 'com.example.demo.configuration.UserInfo' available: expected single matching bean but found 2: userInfo,userInfo2
        //  found 2: userInfo,userInfo2 存储了两个bean ,通过类型不知道获取哪个
        /*
        UserInfo userInfo = (UserInfo) context.getBean(UserInfo.class);
        System.out.println(userInfo.toString());
         */


        /**
         * 下面的都是, 从上下文中获取bean
         */
//        UserComponent userComponent = context.getBean(UserComponent.class);
//        userComponent.doComponent();


//        UserController  userController= context.getBean(UserController.class);
//        userController.doController();

        /*
        // 根据 类型 获取Bean
        UserService userService = context.getBean(UserService.class);
        userService.doService();
        // 根据 Bean 名称 获取
        UserService userService1 = (UserService)context.getBean("userService");
        userService1.doService();

        UserResposittory userResposittory = context.getBean(UserResposittory.class);
        userResposittory.doRepository();

        UserComponent userComponent = context.getBean(UserComponent.class);
        userResposittory.doRepository();

        UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
        userConfiguration.doConfig();
        */


    }

}
